<template>
	<div class="page">
		<div class="header">
			<div class="header-con">
				<div class="header-title">Home</div>
				<div class="header-back">
					<router-link to="/register">
						<span class="back" @click="clearSessionStorage">
							<i class="iconfont">&#xe604;</i> 注销
						</span>
					</router-link>
				</div>
			</div>
		</div>
		<div class="select-body">
			<div class="container">
				<div class="game-wrapper" ref="gameWrapper">
					<div class="game-box" v-for="(game,index) in gameLists" :key="index" @click="choseGame(game.ugid)">
						<img src="./../../static/img/game.png" alt="">
						<div class="text">{{game.gname}}</div>
					</div>
					<div class="game-box" @click="showAddGame" v-if="!visible_addPanel" ref="gameBox">
						<div class="add">+</div>
					</div>
				</div>
			</div>
			<div class="game-add-con" v-if="visible_addPanel">
				<div class="game-add">
					<div class="title">
						<span>添加游戏</span>
						<span class="close-btn" @click="addPanel_display">&times;</span>
					</div>
					<div class="content">
						<div class="game-item" v-for="(game,index) in notAddedGameLists" :key="index">
							<span>{{game.gname}}</span>
							<span class="add-btn" @click="addGameToUser(index)">添加</span>
						</div>
					</div>
				</div>

			</div>
		</div>
		<div class="footer">
			<div class="footer-con">用户 </div>
		</div>
	</div>
</template>

<script>
import Axios from "Axios";
import { mapGetters, mapMutations } from 'vuex';
import {
	UPDATE_SESSIONSTORAGE,
	CLEAR_SESSIONSTORAGE,
} from './../store/mutation-types';

export default {
	name: 'Home',
	data() {
		return {
			gameLists: [],
			notAddedGameLists: [],
			visible_addPanel: false,
		}
	},
	created() {
		this.getGameList();
	},
	mounted() {
		let gameWrapper = this.$refs.gameWrapper;
		let gameBox = this.$refs.gameBox;
		let parentNode = gameWrapper.parentNode;
		let margin = 0;
		if (gameBox.currentStyle) {
			margin = gameBox.currentStyle.margin;
			margin = 0;
		} else {
			let obj = window.getComputedStyle(gameBox, null)
			margin = obj.margin;
			if (margin) {
				margin = parseInt(margin.replace('px', ''));
			}
		}
		let itemWidth = gameBox.offsetWidth + 2 * margin;
		gameWrapper.style.width = Math.floor(parentNode.offsetWidth / itemWidth) * itemWidth + 'px';
	},
	computed: {
		...mapGetters([
			'getUserId'
		]),
		addPanelShow() {
			return this.visible_addPanel
		},
	},
	methods: {
		...mapMutations([
			UPDATE_SESSIONSTORAGE,
			CLEAR_SESSIONSTORAGE,
		]),
		// 清除sessionStorage中指定内容
		clearSessionStorage() {
			this.$store.commit(CLEAR_SESSIONSTORAGE, {
				clearItems: [
					'userId',
					'userName',
					'token',
					'ugId'
				]
			});
		},
		getGameList() {
			Axios({
				url: "/apis/gameList",
				params: {
					userid: this.getUserId
				}
			}).then((response) => {
				let { code, data, msg } = response.data;
				if (code == 0) {
					this.gameLists = data;
				}
			})
		},
		choseGame(ugid) {
			this.UPDATE_SESSIONSTORAGE({
				storageItems: [
					{ key: 'ugId', name: ugid }
				]
			})
			this.$router.push('mines')
		},
		addPanel_display() {
			this.visible_addPanel = !this.visible_addPanel;
		},
		showAddGame() {
			this.addPanel_display();
			let userid = this.getUserId;
			Axios({
				url: 'apis/getUserNotAddedGame',
				params: {
					userid,
				}
			}).then((response) => {
				let { code, data, msg } = response.data;
				if (code == 0) {
					this.notAddedGameLists = data;
				}
			})
		},
		addGameToUser(index) {

			let gameid = this.notAddedGameLists[index].id;
			let userid = this.getUserId;
			Axios({
				url: './apis/usergameRelationReg',
				params: {
					userid, gameid
				}
			}).then(response => {
				let { code, data, msg } = response.data;
				if (code == 0) {
					console.log(response)
					let temp = this.notAddedGameLists.splice(index, 1);
					delete temp[0].id;
					temp[0].ugid =data.id
					this.gameLists = [].concat(this.gameLists, temp);

				}
			})
		},
	}
}
</script>

<style lang="scss" scoped>
.page {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  .header,
  .footer {
    width: inherit;
    height: 40px;
    border: 1px solid #48525a;
    border-left: none;
    border-right: none;
    color: azure;
  }
  /*页头部样式*/
  .header-con {
    width: 100%;
    height: inherit;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    .header-back {
      width: auto;
      height: inherit;
      padding: 0 5px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .back {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      padding: 2px 5px;
      border-radius: 2px;
      background: #545159;
      cursor: pointer;
      color: #fbfbfb;
      > i {
        color: #29f5a6;
      }
    }
    .header-title {
      height: inherit;
      flex: 1;
      display: flex;
      padding: 0 5px;
      justify-content: flex-start;
      align-items: center;
    }
  }
  /*页尾部样式*/
  .footer-con {
    width: 100%;
    height: inherit;
    display: flex;
    flex-direction: row;
    align-items: center;
  }
  .select-body {
    flex: 1;
    position: relative;
    .container {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: center;
      align-items: flex-start;
    }
  }
  .game-wrapper {
    display: inline;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .game-box {
    width: 100px;
    height: 100px;
    margin: 10px;
    display: flex;
    flex-direction: column;
    color: #fff;
    user-select: none;
    > img {
      width: 100%;
    }
    .text {
      width: 100%;
      display: flex;
      justify-content: center;
    }
    .add {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 50px;
    }
  }
}
.game-add-con {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  .game-add {
    width: 100%;
    height: 50%;
    position: absolute;
    bottom: 0;
    background-color: #2b3042;
    display: flex;
    flex-direction: column;
    .title {
      height: 40px;
      line-height: 40px;
      padding: 0 8px;
      border-bottom: 1px solid #48525a;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      color: #ffc107;
      .close-btn {
        display: block;
        width: 22px;
        height: 22px;
        text-align: center;
        line-height: 22px;
        cursor: pointer;
        user-select: none;
      }
    }
    .content {
      flex: 1;
      overflow: auto;
      .game-item {
        height: 40px;
        padding: 0 10px;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        color: #fbfbfb;
        border-bottom: 1px solid #48525a;
        .add-btn {
          display: block;
          width: 50px;
          height: 22px;
          line-height: 22px;
          background: #48525a;
          text-align: center;
          cursor: pointer;
        }
      }
    }
  }
}
</style>